--- title: OpenHSI Class keywords: fastai sidebar: home_sidebar summary: "The bridge between camera implementations and the rest of `openhsi` land." description: "The bridge between camera implementations and the rest of `openhsi` land." nb_path: "nbs/01_capture.ipynb" ---
{% raw %}
/Users/eway/.pyenv/versions/3.8.3/lib/python3.8/site-packages/pandas/compat/__init__.py:97: UserWarning: Could not import the lzma module. Your installed Python is incomplete. Attempting to use lzma compression will result in a RuntimeError.
  warnings.warn(msg)
{% endraw %} {% raw %}
{% endraw %} {% raw %}
{% endraw %} {% raw %}

class OpenHSI[source]

OpenHSI(n_lines:int=16, processing_lvl:int=-1, preserve_raw:bool=False, json_path:str=None, pkl_path:str=None, print_settings=False) :: DataCube

Base Class for the OpenHSI Camera.
{% endraw %} {% raw %}
{% endraw %} {% raw %}

OpenHSI.collect[source]

OpenHSI.collect()

Collect the hyperspectral datacube.

OpenHSI.avgNimgs[source]

OpenHSI.avgNimgs(n)

Take `n` images and find the average
{% endraw %}

{% include warning.html content='Running in notebook slow downs the camera more than running in a script. ' %}

To add a custom camera, five methods need to be defined in a class to:

  1. Initialise camera __init__, and
  2. Open camera start_cam, and
  3. Close camera stop_cam, and
  4. Capture a picture as a numpy array get_img, and
  5. Update the exposure settings set_exposure, and
  6. [Optional] Poll the camera temperature get_temp.

By inheriting from the OpenHSI class, all the methods to load settings/calibration files, collect datacube, saving data to NetCDF, and viewing as RGB are integrated. Furthermore, the custom camera class can be passed to a SettingsBuilder class for calibration.

For example, we implement a simulated camera below.

{% raw %}

class SimulatedCamera[source]

SimulatedCamera(img_path:str=None, n_lines:int=16, processing_lvl:int=-1, preserve_raw:bool=False, json_path:str=None, pkl_path:str=None, print_settings=False) :: OpenHSI

Simulated camera using an RGB image as an input. Hyperspectral data is produced using CIE XYZ matching functions.
{% endraw %} {% raw %}
{% endraw %} {% raw %}

SimulatedCamera.rgb2xyz_matching_funcs[source]

SimulatedCamera.rgb2xyz_matching_funcs(rgb:ndarray)

convert an RGB value to a pseudo-spectra with the CIE XYZ matching functions.
{% endraw %} {% raw %}
with SimulatedCamera(img_path="assets/great_hall_slide.png", n_lines=1028, processing_lvl = 3, 
                     json_path="assets/cam_settings.json",pkl_path="assets/cam_calibration.pkl") as cam:
    cam.collect()
    fig = cam.show(plot_lib="matplotlib",hist_eq=True)
100%|██████████| 1028/1028 [00:21<00:00, 48.23it/s]
{% endraw %} {% raw %}
fig.opts(fig_inches=7,title="simulated hyperspectral datacube")
{% endraw %}

Each RGB value is converted into a pseudo-spectra by using the CIE XYZ matching functions.

{% raw %}
Text(0, 0.5, 'CIE XYZ value')
{% endraw %}

Loading and processing datacubes further

{% include warning.html content='ProcessRawDatacube only works for raw data captured using processing_lvl = -1. Processing from other levels are still under development.' %}

{% raw %}

class ProcessRawDatacube[source]

ProcessRawDatacube(fname:str, processing_lvl:int, json_path:str, pkl_path:str, old_style:bool=False) :: OpenHSI

Post-process datacubes
{% endraw %} {% raw %}
{% endraw %} {% raw %}
json_path='../calibration_files/OpenHSI-16_settings_Mono8_bin2.json'
pkl_path='../calibration_files/OpenHSI-16_calibration_Mono8_bin2_window.pkl'

with ProcessRawDatacube(fname = "../../Downloads/16_pvn1_bin2_10ms2022_01_13-04_22_25.nc", processing_lvl=7,
                        json_path=json_path, pkl_path=pkl_path) as cam:
    cam.collect()
100%|██████████| 1000/1000 [00:08<00:00, 113.41it/s]
{% endraw %} {% raw %}
fig = cam.show(hist_eq=True)
fig
{% endraw %}